//校验登录
checkLogin()

//用户名渲染
usernameRander()

//退出登录
logoutLogin()

//由于渲染的数据较多 这里选择抽取

//顶部数据渲染
function randerOverview(overview) {
    //返回的对象属性名和标签的class名一样  
    Object.keys(overview).forEach(key => {
        document.querySelector(`.${key}`).innerHTML = overview[key]
    })

}

//每月薪资渲染
function salaryYear(year) {
    const yearSalary = echarts.init(document.querySelector('#line'))

    const option = {
        title: {
            text: '2022全学科薪资走势',
            left: '5%',
            top: '5%'
        },
        grid: {
            top: '20%'
        },
        xAxis: {
            type: 'category',
            // boundaryGap: false,
            data: year.map(e => e['month']),
            axisLine: {
                lineStyle: {
                    color: "#ccc",
                    type: "dashed"
                }
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: "dashed"
                }
            }

        },
        series: [
            {
                data: year.map(e => e['salary']),
                type: 'line',
                areaStyle: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 1,
                        x2: 0,
                        y2: 0,
                        colorStops: [
                            {
                                offset: 0,
                                color: 'rgba(255,255,255,0)' // 0% 处的颜色
                            },
                            {
                                offset: 0.9,
                                color: '#b2d7f7' // 100% 处的颜色
                            }
                        ],

                    }

                },

                smooth: true,

                symbolSize: 10,
                lineStyle: {
                    width: 6,
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                        colorStops: [
                            {
                                offset: 0.5,
                                color: '#43a7f3' // 0% 处的颜色
                            },
                            {
                                offset: 1,
                                color: '#5987f5' // 100% 处的颜色
                            }
                        ],
                        global: false // 缺省为 false
                    }

                },



            }
        ],
        tooltip: {
            show: true,
            trigger: 'axis'
        }
    };
    yearSalary.setOption(option)
}

//薪资分布渲染
function salaryDistribution(salaryData) {
    const salary = document.querySelector('#salary')
    const echart = echarts.init(salary)
    const option = {

        title: {
            text: '薪资分布',
            left: '5%',
            top: '5%'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {

            bottom: '5%'
        },
        series: [
            {
                radius: ["55%", "70%"],
                name: '班级薪资分布',
                type: 'pie',
                avoidLabelOverlap: true,
                itemStyle: {
                    borderRadius: 15,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },

                labelLine: {
                    show: false
                },
                data: salaryData.map(e => {
                    const { label, g_count, b_count } = e
                    return { name: label, value: g_count + b_count }
                })
            }
        ],
        color: ['#ffab00', '#4aa0ff', '#0fc4fd', '#00daa2']
    }
    echart.setOption(option)
}

//每组薪资渲染
function groupSalary(groupData) {



    const myCharts = echarts.init(lines)
    const option = {
        title: {

        },
        grid: {
            left: 70,
            top: 30,
            right: 30,
            bottom: 50
        },
        tooltip: {
            //trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            axisLabel: {
                color: '#999'
            },
            data: groupData[1].map(e => e.name),
            axisLine: {
                lineStyle: {
                    color: "#ccc",
                    type: "dashed"
                }
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }

            }
        },
        series: [
            {
                name: '期望薪资',
                //data: [120, 200, 150, 80, 70, 110, 130],
                data: groupData[1].map(e => e.hope_salary),
                type: 'bar',
                itemStyle: {
                    color:
                    {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [
                            {
                                offset: 0,
                                color: '#34D39A' // 0% 处的颜色
                            },
                            {
                                offset: 1,
                                color: 'rgba(52,211,154,.2)' // 100% 处的颜色
                            }
                        ],
                        global: false // 缺省为 false
                    }

                }
            },
            {
                name: '就业薪资',
                // data: [120, 200, 150, 80, 70, 110, 130],
                data: groupData[1].map(e => e.salary),
                type: 'bar',
                itemStyle: {
                    color:
                    {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [
                            {
                                offset: 0,
                                color: '#499fee' // 0% 处的颜色
                            },
                            {
                                offset: 1,
                                color: 'rgba(73,159,238,.2)' // 100% 处的颜色
                            }
                        ],
                        global: false // 缺省为 false
                    }

                }
            }
        ]
    }
    myCharts.setOption(option)

    const btns = document.querySelector('#btns')
    btns.addEventListener('click', e => {
        if (e.target.tagName === 'BUTTON') {
            //btn-blue
            document.querySelector('#btns .btn-blue').classList.remove('btn-blue')
            e.target.classList.add('btn-blue')
            const index = e.target.innerHTML
            option['xAxis']['data'] = groupData[index].map(e => e.name)
            option['series'][0]['data'] = groupData[index].map(e => e.hope_salary)
            option['series'][1]['data'] = groupData[index].map(e => e.salary)
            myCharts.setOption(option)
        }
    })




}

//薪资分布 区分男女
function salaryDistributionType(salaryData) {
    const gender = document.querySelector('#gender')
    const myCharts = echarts.init(gender)
    const option = {

        title: [
            {
                text: '男女薪资分布',
                left: 10,
                top: 10,
                textStyle: {
                    fontSize: 16
                },
            },
            {
                text: '男生',
                left: '50%',
                top: '45%',
                textAlign: 'center',
                textStyle: {
                    fontSize: 12
                },
            },
            {
                text: '女生',
                left: '50%',
                top: '85%',
                textAlign: 'center',
                textStyle: {
                    fontSize: 12
                },
            }],
        tooltip: {
            trigger: 'item'
        },

        series: [
            {
                name: '男生',
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '30%'],
                data: salaryData.map(e => {
                    const { label, g_count } = e
                    return { name: label, value: g_count }
                }),
                color: ['#ff9d00', '#2f99ff', '#00bfff', '#00d796']
            },
            {
                name: '女生',
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '70%'],
                data: salaryData.map(e => {
                    const { label, b_count } = e
                    return { name: label, value: b_count }
                }),
                color: ['#ff9d00', '#2f99ff', '#00bfff', '#00d796']
            },

        ]
    }
    myCharts.setOption(option)
}

//籍贯分布
function renderProvince(provinceData) {
    const dom = document.querySelector('#map')
    const myEchart = echarts.init(dom)
    const dataList = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 0 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 },
    ]

    //筛选数据
    dataList.forEach(ele => {
        const res = provinceData.find(k => {
            return k.name.includes(ele.name)
        })
        if (res) {
            ele.value = res.value
        }
    })
    const option = {
        title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} 位学员',
            borderColor: 'transparent',
            backgroundColor: 'rgba(0,0,0,0.5)',
            textStyle: {
                color: '#fff',
            },
        },
        visualMap: {
            min: 0,
            max: 6,
            left: 'left',
            bottom: '20',
            text: ['6', '0'],
            inRange: {
                color: ['#ffffff', '#0075F0'],
            },
            show: true,
            left: 40,
        },
        geo: {
            map: 'china',
            roam: false,
            zoom: 1.0,
            label: {
                normal: {
                    show: true,
                    fontSize: '10',
                    color: 'rgba(0,0,0,0.7)',
                },
            },
            itemStyle: {
                normal: {
                    borderColor: 'rgba(0, 0, 0, 0.2)',
                    color: '#e0ffff',
                },
                emphasis: {
                    areaColor: '#34D39A',
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    shadowBlur: 20,
                    borderWidth: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)',
                },
            },
        },
        series: [
            {
                name: '籍贯分布',
                type: 'map',
                geoIndex: 0,
                data: dataList,
            },
        ],
    }
    myEchart.setOption(option)
}



//首页渲染 获取数据
async function getData() {

    //获取token
    //调接口
    const res = await axios({
        url: '/dashboard'
    })

    const { overview, year, salaryData, groupData, provinceData } = res.data
    randerOverview(overview)
    salaryYear(year)
    salaryDistribution(salaryData)
    groupSalary(groupData)
    salaryDistributionType(salaryData)
    renderProvince(provinceData)





}

getData()